package com.redis.springbootredis.service.impl;

import com.redis.springbootredis.dao.OrderTestDao;
import com.redis.springbootredis.entity.OrderTest;
import com.redis.springbootredis.service.OrderTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author
 * @create 2020-10-30 12:02
 */
@Service
public class OrderTestServiceImpl implements OrderTestService {

    @Autowired
    OrderTestDao orderTestDao;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public OrderTest getOrderTest(String userName, String password) {
        OrderTest orderTest = orderTestDao.selectOrderTest(userName,password);
        return orderTest;
    }

    @Override
    public void serviceStart() {
        ListOperations<String, Object> opsForList = redisTemplate.opsForList();
        for(int i = 1 ; i <= 5000 ; i++){
            opsForList.rightPush("list",i);
        }
    }

    @Override
    public void serviceEnd() {
        ListOperations<String, Object> opsForList = redisTemplate.opsForList();
        Object obj = opsForList.leftPop("list");
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String formatdate = sim.format(date);
        if(obj!=null){
            orderTestDao.insertOrderTest(0,"fzt","abc",formatdate);
            System.out.println("end");
        }
        System.out.println("秒杀结束");
    }

    @Override
    public void addOrderTest(Integer oid, String userName, String password, String date) {
        orderTestDao.insertOrderTest(oid, userName, password, date);
    }
}
